import path from "path";
import { CleanWebpackPlugin } from "clean-webpack-plugin";
import CopyPlugin from "copy-webpack-plugin";
import fs from "fs";
import { copyDir } from "./webpack-utils.js";

// const tsArr = [];
// const getAllTs = (dir) => {
//   const list = fs.readdirSync(dir);
//   list.forEach((val) => {
//     const cur = path.join(dir, val);
//     if (fs.statSync(cur).isDirectory()) getAllTs(cur);
//     else if (/^[^\.]*\.ts$/.test(cur)) tsArr.push(cur);
//   });
// };

// getAllTs(path.resolve("./src/first"));

// const getEntries = (arr) => {
//   const obj = {};
//   arr.map((val) => {
//     const entryName = path.basename(val, ".ts");
//     obj[entryName] = val;
//   });
//   return obj;
// };

// const entry = getEntries(tsArr);

// console.log(entry);

//   from: path.resolve("src/template"),
//   to: path.resolve("dist/template"),
if (!fs.existsSync("dist/template")) fs.mkdirSync("dist/template");
copyDir(path.resolve("src/template"), path.resolve("dist/template"));

export default {
  entry: path.resolve("./src/second/test/run.ts"),
  // entry: path.resolve("./src/second/src/Main.ts"),
  target: "node",
  output: {
    path: path.resolve("./", "dist/second"), // Output directory
    environment: {
      arrowFunction: false,
    },
  },
  resolve: {
    extensions: [".ts", ".js"], // Include TypeScript and JavaScript files
  },
  module: {
    rules: [
      {
        test: /\.ts$/,
        use: "ts-loader",
        exclude: /node_modules/,
      },
    ],
  },
  plugins: [
    new CleanWebpackPlugin(),
    new CopyPlugin({
      patterns: [
        {
          from: path.resolve("src/second/test/package.json"),
          to: path.resolve("dist/second/package.json"),
        },
      ],
    }),
  ],
};
